package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.PositionDao;
import com.bdqn.oa.entity.Position;
import com.bdqn.oa.utils.DruidUtils;
import com.bdqn.oa.utils.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zhowei
 * @date 2022/7/20 17:14
 */
public class PositionDaoImpl implements PositionDao {
    private QueryRunner queryRunner = new QueryRunner();
    /**
     * 增加部门下的岗位（通过岗位名）
     *
     * @param position
     * @return
     */
    @Override
    public int insertPostion(Position position) throws SQLException {
      return queryRunner.update(DruidUtils.getConnection(),"insert into position (`name`, department_id) value (?,?);",position.getName(),position.getDepartmentId());
    }

    /**
     * 通过岗位名查找岗位(得到相同岗位名称的部门id)
     *
     * @param name
     * @return
     */
    @Override
    public List<Position> getPositonByName(String name) throws SQLException {
        return queryRunner.query(DruidUtils.getConnection(),"select * from position where name=?",new BeanListHandler<Position>(Position.class),name);
    }

    /**
     * 查找某部门下的所有岗位
     *
     * @param DepId
     * @return
     */
    @Override
    public List<Position> getPositonByDepIdOfList(Integer DepId) throws SQLException {
        return queryRunner.query(DruidUtils.getConnection(),"select id,name from position where department_id=?",new BeanListHandler<Position>(Position.class),DepId);
    }

    /**
     * 查找某部门下的所有岗位的分页详情
     *
     * @param DepId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<Position> getPositonByDepIdOfPage(Integer DepId, long pageNumber, long pageSize) throws SQLException {
        Page<Position>positionPage = new Page<Position>();
        positionPage.setPageSize(pageSize);
        positionPage.setPageNumber(pageNumber);
        positionPage.setData(queryRunner.query(DruidUtils.getConnection(),"select * from position where department_id=? limit ?,?",new BeanListHandler<Position>(Position.class)
                ,DepId,(pageNumber-1)*pageSize,pageSize));
        return positionPage;
    }

    /**
     * 查找某部门id下的某岗位
     *
     * @param DepId
     * @param name
     * @return
     */
    @Override
    public Position getPositionByDepIdAndName(Integer DepId, String name) throws SQLException {
        return queryRunner.query(DruidUtils.getConnection(),"select * from position where department_id=? and name=?",new BeanHandler<Position>(Position.class),DepId,name);
    }

    /**
     * 通过岗位id查找岗位
     *
     * @param id
     * @return
     */
    @Override
    public Position getPositionById(Integer id) throws SQLException {
        return queryRunner.query(DruidUtils.getConnection(),"select * from position where id=?",new BeanHandler<Position>(Position.class),id);
    }

    /**
     * 修改某部门下的岗位名称
     *
     * @param position
     * @return
     */
    @Override
    public int updatePositionName(Position position) throws SQLException {
        return queryRunner.update(DruidUtils.getConnection(),"update position set name=? where department_id=?",position.getName(),position.getDepartmentId());
    }

    /**
     * 岗位分页查看
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<Position> getPositionByPage(long pageNumber, long pageSize) throws SQLException {
        Page<Position>positionPage = new Page<Position>();
        positionPage.setPageSize(pageSize);
        positionPage.setPageNumber(pageNumber);
        positionPage.setData(queryRunner.query(DruidUtils.getConnection(),"select * from position limit ?,?",new BeanListHandler<Position>(Position.class)
                ,(pageNumber-1)*pageSize,pageSize));
        return positionPage;
    }

    /**
     * 获取岗位数量
     *
     * @return
     */
    @Override
    public int countPosition() throws SQLException {
        List<Position> positionList = queryRunner.query(DruidUtils.getConnection(),"select * from position",new BeanListHandler<Position>(Position.class));
        return positionList.size();
    }

    /**
     * 获取部门下的岗位数量
     *
     * @param DepId
     * @return
     */
    @Override
    public int countPositionByDepId(Integer DepId) throws SQLException {
        List<Position> positionList = queryRunner.query(DruidUtils.getConnection(),"select * from position where department_id=?",new BeanListHandler<Position>(Position.class),DepId);
        return positionList.size();
    }
}
